﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Xml;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace MenuGame2DXNA
{
    public class GroupLayout : VisibleGameEntity
    {

      
        /// <summary>
        /// Initializes a new instance of the <see cref="MyXNAButton"/> class.
        /// </summary>
        /// <param name="_contentManager">The _content manager.</param>
        /// <param name="StrXml">The STR XML.</param>
        /// <param name="rootFolder">The root folder.</param>
        public GroupLayout(ContentManager _contentManager, string FileNameXml,string rootFolder)
        {
            XmlDocument grouplayout = new XmlDocument();
            grouplayout.Load(FileNameXml);
             ObjectVisible = new List<Sprite>();
            XmlElement textures = (XmlElement)grouplayout.SelectSingleNode("GroupLayout/textures");
             if (textures != null)
                foreach (XmlElement texture in textures.ChildNodes)
                {
                    XmlElement x = (XmlElement)texture.SelectSingleNode("x");
                    XmlElement y = (XmlElement)texture.SelectSingleNode("y");
                    Vector2 _topLeft =new Vector2( float.Parse(x.InnerText),float.Parse(y.InnerText));
                    XmlElement width = (XmlElement)texture.SelectSingleNode("width");
                    XmlElement heigh = (XmlElement)texture.SelectSingleNode("heigh");
                    float widthsprite = float.Parse(width.InnerText);
                     float heighsprite = float.Parse(heigh.InnerText);
                     XmlElement xmlAnpha= (XmlElement)texture.SelectSingleNode("Anpha");                 
                     float Anpha = float.Parse(xmlAnpha.InnerText);

                     XmlElement Nametexture =(XmlElement) texture.SelectSingleNode("NameTexture");                 
                    

                    List<Texture2D> ListTexture =  new List<Texture2D>();
                    ListTexture.Add(_contentManager.Load<Texture2D>(Nametexture.InnerText));

                    Sprite temSprite = new AutomatedSprite(ListTexture.ToArray(), _topLeft);
                    temSprite.Size = new Vector2(widthsprite, heighsprite);
                    temSprite.Name = texture.GetAttribute("name");
                    if(Anpha < 255)
                    temSprite.Color = new Color(0, 0, 0, Anpha);
                     ObjectVisible.Add(temSprite);           
                }
            nObjectVisible = ObjectVisible.Count;
           
        }

        public Sprite this[string name]
        {
            get
            {
                foreach (Sprite s in ObjectVisible)
                {
                    if (s.Name == name)
                        return s;
                }
                return null;
            }

        }
         public Sprite this[int index]
        {
            get
            {
                for (int i =0;i<nObjectVisible;i++)
                {
                    if(index == i)
                        return ObjectVisible[i];
                }
                return null;
            }
            
        }
        public int getCount()
        {
            return nObjectVisible;
        }
        public override void Draw(GameTime gameTime, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {

            for (int i = 0; i < nObjectVisible; i++)
            {
                ObjectVisible[i].Draw(gameTime, spriteBatch);
            }
            base.Draw(gameTime, spriteBatch);
        }
        public override void UpdatePositon()
        {
            for (int i = 0; i < nObjectVisible; i++)
                ObjectVisible[i].Position = new Vector2(ObjectVisible[i].PositionStart.X + TopLeft.X,
                    ObjectVisible[i].PositionStart.Y + TopLeft.Y);
                
        }
        public void UpdaterealPositon(Vector2 position)
        {
            for (int i = 0; i < nObjectVisible; i++)
            {
                ObjectVisible[i].PositionStart += (position - PositionStart);
            }
            PositionStart = position;
        }

    }
}
